/*
 * Copyright 2004-2006 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.sysframe.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.companyname.common.sysframe.define.OrgStatus;

/**
 * <p>Title: 机构 </p>
 * <p>Description: 机构 Org </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: 公司名</p>
 * @ $Author: 作者名 $
 * @ $Date: 创建日期 $
 * @ $Revision: 1.0 $
 * @ created in 创建日期
 *
 */

public class Org extends Unit
                 implements Serializable
{
        static Logger logger = Logger.getLogger(Org.class);
        
        // 父机构
        private Org superOrg;
        
        // 子机构
        private Set subOrgs;
        
        // 岗位
        private Set posts;

        // name(机构名称)
        private String name;

        // country(国家)
        private String country;

        // province（省）
        private String province;

        // city(市)
        private String city;

        // street（街道）
        private String street;

        // office(房号)
        private String office;

        // link_man(联系人)
        private String linkMan;

        // phone(电话)
        private String phone;

        // fax(传真)
        private String fax;
        
        // 主页
        private String site;

        // email
        private String email;

        // remark(备注)
        private String remark;

        // status(状态：0:作废;1:有效)
        private int status;

        /**
         * 构造函数
         */
        public Org()
        {
                // for hibernate
        }
        
        public Org(String code, String name)
        {
                this.code = code;
                this.name = name;
                this.country = "";
                this.province = "";
                this.city = "";
                this.street = "";
                this.office = "";
                this.linkMan = "";
                this.phone = "";
                this.fax = "";
                this.email = "";
                this.site = "";
                this.remark = "";
                this.status = OrgStatus.VALID;
        }

        public Org(String code,
                   String name,
                   String pri)
        {
                this.code = code;
                this.name = name;
                this.pri = pri;
                this.country = "";
                this.province = "";
                this.city = "";
                this.street = "";
                this.office = "";
                this.linkMan = "";
                this.phone = "";
                this.fax = "";
                this.email = "";
                this.site = "";
                this.remark = "";
                this.status = OrgStatus.VALID;
        }

        public Set getSubOrgs()
        {
                return subOrgs;
        }

        public void setSubOrgs(Set subOrgs)
        {
                this.subOrgs = subOrgs;
        }
        
        public void addOrg(Org org)
        {
                if(this.subOrgs == null)
                {
                        this.subOrgs = new LinkedHashSet();
                }
                this.subOrgs.add(org);
                org.setSuperOrg(this);
        }
        
        public void addSubOrgs(Set subOrgs)
        {
                if(this.subOrgs == null)
                {
                        this.subOrgs = new LinkedHashSet();
                }
                Object[] orgs = subOrgs.toArray();
                for(int i = 0; i < orgs.length; i ++)
                {
                        Org org = (Org)orgs[i];
                        this.subOrgs.add(org);
                        org.setSuperOrg(this);
                }
        }
        
        // 有效的子机构
        public Set getValidSubOrgs()
        {
                Set vSubOrgs = new LinkedHashSet();
                Object[] orgs = this.subOrgs.toArray();
                for(int i = 0; i < orgs.length; i ++)
                {
                        Org org = (Org)orgs[i];
                        if(org.getStatus() == OrgStatus.VALID)
                        {
                                vSubOrgs.add(org);
                        }
                }
                return vSubOrgs;
        }

        public Org getSuperOrg()
        {
                return superOrg;
        }

        public void setSuperOrg(Org superOrg)
        {
                this.superOrg = superOrg;
        }

        // name(机构名称)
        public String getName()
        {
                return this.name;
        }
        
        // 根据深度获取全名 
        public String getFullNameWithLength(int length)
        {
                if(length > 1 && this.superOrg != null)
                {
                        length--;
                        return this.superOrg.getFullNameWithLength(length) + this.name;
                }
                else
                {
                        return this.name;
                }
        }

        public void setName(String name)
        {
                this.name = name;
        }

        // country(国家)
        public String getCountry()
        {
                return this.country;
        }

        public void setCountry(String country)
        {
                this.country = country;
        }

        // province（省）
        public String getProvince()
        {
                return this.province;
        }

        public void setProvince(String province)
        {
                this.province = province;
        }

        // city(市)
        public String getCity()
        {
                return this.city;
        }

        public void setCity(String city)
        {
                this.city = city;
        }

        // street（街道）
        public String getStreet()
        {
                return this.street;
        }

        public void setStreet(String street)
        {
                this.street = street;
        }

        // office(房号)
        public String getOffice()
        {
                return this.office;
        }

        public void setOffice(String office)
        {
                this.office = office;
        }
                
        public String getLinkMan()
        {
                return linkMan;
        }

        public void setLinkMan(String linkMan)
        {
                this.linkMan = linkMan;
        }

        // phone(电话)
        public String getPhone()
        {
                return this.phone;
        }

        public void setPhone(String phone)
        {
                this.phone = phone;
        }

        // fax(传真)
        public String getFax()
        {
                return this.fax;
        }

        public void setFax(String fax)
        {
                this.fax = fax;
        }

        // email
        public String getEmail()
        {
                return this.email;
        }

        public void setEmail(String email)
        {
                this.email = email;
        }

        // remark(备注)
        public String getRemark()
        {
                return this.remark;
        }

        public void setRemark(String remark)
        {
                this.remark = remark;
        }
        
        public Set getPosts()
        {
                return posts;
        }

        public void setPosts(Set posts)
        {
                this.posts = posts;
        }
        
        // 取得机构下面角色对应的岗位
        public Post getPostByRoleCode(String roleCode)
        {
                if(this.posts != null)
                {
                        Object[] ps = this.posts.toArray();
                        for(int i = 0; i < ps.length; i ++)
                        {
                                Post p = (Post)ps[i];
                                Role role = p.getRole();
                                if(role.getCode().equals(roleCode))
                                {
                                        return p;
                                }
                        }
                        return null;
                }
                else
                {
                        return null;
                }
        }

        public String getSite()
        {
                return site;
        }

        public void setSite(String site)
        {
                this.site = site;
        }

        // status(状态：0:作废;1:有效)
        public int getStatus()
        {
                return this.status;
        }

        public void setStatus(int status)
        {
                this.status = status;
        }
        
        public String getStatusName()
        {
                return OrgStatus.NAMES[this.status];
        }
        
        /** 获取所有子孙节点 */
        public List getAllSubOrgs()
        {
                return this.getAllSubOrgs(this);

        }
        /** 递归获取子孙节点model */
        private List getAllSubOrgs(Org parentOrg)
        {
                List tsubOrgs = new Vector(parentOrg.getSubOrgs());
                List allSubOrgs = new Vector();
                if(tsubOrgs.size() != 0)
                {

                        for(int i = 0; i <tsubOrgs.size(); i ++)
                        {
                                Org org = (Org)tsubOrgs.get(i);
                                if(org.getStatus() == OrgStatus.VALID)
                                {
                                        allSubOrgs.add(org);
                                        allSubOrgs.addAll(this.getAllSubOrgs(org));
                                }
                        }
                }
                return allSubOrgs;
        }
        
        // 取得到根节点的路径
        public List getUnits2Root()
        {
                return this.getParentOrgs(new ArrayList());
        }
        
        // 取得祖先机构
        public Org getSuperOrgByCode(String superOrgCode)
        {
                List superOrgs = this.getUnits2Root();
                for(int i = 0; i < superOrgs.size(); i ++)
                {
                        Org superOrg = (Org)superOrgs.get(i);
                        if(superOrg.getCode().equals(superOrgCode))
                        {
                                return superOrg;
                        }
                }
                return null;
        
        }
        
        private List getParentOrgs(List orgs)
        {
                orgs.add(this);
                if(this.superOrg == null)
                {
                        return orgs;
                }
                else
                {
                        return this.superOrg.getParentOrgs(orgs);
                }
        }

        /** 采用框架提供的操作日志必须填写的项目 */
        public void setModeldetail()
        {
                this.setModelFieldDetail("id",this.getId());
                this.setModelFieldDetail("业务主键",this.code);
                this.setModelFieldDetail("机构名称",this.name);
                if(this.superOrg != null)
                {
                        this.setModelFieldDetail("父机构",this.superOrg.getName());
                }
                this.setModelFieldDetail("国家",this.country);
                this.setModelFieldDetail("省",this.province);
                this.setModelFieldDetail("市",this.city);
                this.setModelFieldDetail("街道",this.street);
                this.setModelFieldDetail("房号",this.office);
                this.setModelFieldDetail("联系人",this.linkMan);
                this.setModelFieldDetail("电话",this.phone);
                this.setModelFieldDetail("传真",this.fax);
                this.setModelFieldDetail("主页",this.site);
                this.setModelFieldDetail("email",this.email);
                this.setModelFieldDetail("优先级",this.pri);
                this.setModelFieldDetail("状态",this.getStatusName());
        }

}
